package com.me.bbs.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.me.bbs.anno.RequireLogin;
import com.me.bbs.common.Resp;
import com.me.bbs.common.SessionUtils;
import com.me.bbs.dto.ForumFollowingDTO;
import com.me.bbs.dto.UserFollowingDTO;
import com.me.bbs.entity.ForumFollowing;
import com.me.bbs.entity.UserFollowing;
import com.me.bbs.service.IUserFollowingService;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lm
 * @since 2023-01-22
 */
@RestController
@RequestMapping("/userFollowing")
public class UserFollowingController {
    @Resource
    IUserFollowingService userFollowingService;

    @PostMapping("/follow/{uid}")
    @RequireLogin
    public Resp<?> follow(@PathVariable("uid") Long uid) throws Exception {
        Long suid = (Long) SessionUtils.getAttribute("user");
        UserFollowing uf = new UserFollowing();
        uf.setFollowedUser(uid);
        uf.setUser(suid);
        userFollowingService.follow(uf, true);
        return Resp.success();
    }

    @DeleteMapping("/follow/{uid}")
    @RequireLogin
    public Resp<?> unfollow(@PathVariable("uid") Long uid) throws Exception {
        Long suid = (Long) SessionUtils.getAttribute("user");
        UserFollowing uf = new UserFollowing();
        uf.setFollowedUser(uid);
        uf.setUser(suid);
        userFollowingService.follow(uf, false);
        return Resp.success();
    }

    @PostMapping("/getFollowingUser")
    public Resp<?> getFollowingUser(@RequestParam("current") Long current,
                                    @RequestParam("size") Long size,
                                    @RequestParam("userId") Long userId) {
        LambdaQueryWrapper<UserFollowing> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(userId != null,UserFollowing::getUser, userId);
        Page<UserFollowingDTO> userFollowingDTOPage = userFollowingService
                .getUserFollowingPage(
                        new Page<>(current,size)
                        , wrapper);
        return Resp.success(userFollowingDTOPage, "成功");
    }

    @PostMapping("/getFans")
    public Resp<?> getFans(@RequestParam("current") Long current,
                                    @RequestParam("size") Long size,
                           @RequestParam("userId") Long userId) {
        LambdaQueryWrapper<UserFollowing> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(userId != null,UserFollowing::getFollowedUser, userId);
        UserFollowing userFollowing = new UserFollowing();
        userFollowing.setFollowedUser(userId);
        wrapper.setEntity(userFollowing);
        Page<UserFollowingDTO>  fansPage = userFollowingService
                .getFansPage(
                        new Page<>(current,size)
                        , wrapper);
        return Resp.success(fansPage, "成功");
    }
}
